package com.yyplatform.admin.platform.notice.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yyplatform.admin.infra.utils.DateUtils;
import com.yyplatform.admin.platform.notice.model.dto.SysNoticeUserDTO;
import com.yyplatform.admin.platform.notice.model.dto.SysNoticeUserQueryDTO;
import com.yyplatform.admin.platform.notice.model.entity.SysNoticeEntity;
import com.yyplatform.admin.platform.notice.model.entity.SysNoticeUserEntity;
import com.yyplatform.admin.platform.notice.repository.SysNoticeRepository;
import com.yyplatform.admin.platform.notice.repository.SysNoticeUserRepository;
import com.yyplatform.admin.platform.notice.service.SysNoticeUserService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2025-04-10
 */
@Service
@AllArgsConstructor
@Slf4j
public class SysNoticeUserServiceImpl implements SysNoticeUserService {


    private SysNoticeRepository sysNoticeRepository;
    private SysNoticeUserRepository sysNoticeReadRepository;
    private ModelMapper modelMapper;

    @Override
    public Page<SysNoticeUserDTO> page(Long userId, SysNoticeUserQueryDTO pageQueryDTO) {
        Page<SysNoticeUserEntity> entityPage =
                sysNoticeReadRepository.findPage(pageQueryDTO.getReadFlag(), pageQueryDTO.getDeleteFlag(),
                        pageQueryDTO.getNoticeType(), userId, DateUtils.getBeginDate(pageQueryDTO.getCreateTimeArr()),
                        DateUtils.getEndDate(pageQueryDTO.getCreateTimeArr()),
                        pageQueryDTO.getPageRequestOrderByIdDesc());

        List<SysNoticeUserDTO> list = entityPage.stream().map(e -> modelMapper.map(e, SysNoticeUserDTO.class)).toList();


        List<Long> noticeIdList = list.stream().map(SysNoticeUserDTO::getNoticeId).toList();
        List<SysNoticeEntity> noticeList = sysNoticeRepository.findAllById(noticeIdList);
        Map<Long, SysNoticeEntity> entityMap = new HashMap<>();
        for (SysNoticeEntity sysNoticeEntity : noticeList) {
            entityMap.put(sysNoticeEntity.getId(), sysNoticeEntity);
        }


        for (SysNoticeUserDTO sysNoticeUserDTO : list) {
            SysNoticeEntity sysNoticeEntity = entityMap.get(sysNoticeUserDTO.getNoticeId());
            if (sysNoticeEntity != null) {
                sysNoticeUserDTO.setTitle(sysNoticeEntity.getTitle());
                sysNoticeUserDTO.setContent(sysNoticeEntity.getContent());
                sysNoticeUserDTO.setSendUser(sysNoticeEntity.getCreateBy());
            }
        }

        return new PageImpl<>(list, entityPage.getPageable(), entityPage.getTotalElements());
    }

    @Transactional
    @Override
    public SysNoticeUserDTO get(Long currentUser, Long id) {

        SysNoticeUserEntity entity = sysNoticeReadRepository.getReferenceById(id);
        if (entity.getUserId().longValue() != currentUser.longValue()) {
            throw new RuntimeException("无权限");
        }


        SysNoticeUserDTO sysNoticeUserDTO = modelMapper.map(entity, SysNoticeUserDTO.class);

        SysNoticeEntity noticeEntity = sysNoticeRepository.getReferenceById(sysNoticeUserDTO.getNoticeId());
        sysNoticeUserDTO.setTitle(noticeEntity.getTitle());
        sysNoticeUserDTO.setContent(noticeEntity.getContent());


        entity.setReadFlag(true);
        sysNoticeReadRepository.save(entity);
        sysNoticeReadRepository.flush();

        return sysNoticeUserDTO;
    }


    @Override
    public void delete(Long currentUser, Long id) {
        SysNoticeUserEntity entity = sysNoticeReadRepository.getReferenceById(id);
        if (entity.getUserId().longValue() != currentUser.longValue()) {
            throw new RuntimeException("无权限");
        }

        entity.setDeleteFlag(true);
        sysNoticeReadRepository.save(entity);
    }

    @Override
    public void updateReadFlag(Long currentUser, String ids, boolean readFlag) {
        List<Long> IDS = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        List<SysNoticeUserEntity> list = sysNoticeReadRepository.findAllById(IDS);

        for (SysNoticeUserEntity sysNoticeUserEntity : list) {
            if (sysNoticeUserEntity.getUserId().longValue() == currentUser.longValue()) {
                sysNoticeUserEntity.setReadFlag(readFlag);
            }
        }

        sysNoticeReadRepository.saveAll(list);
    }

    @Override
    public void deleteAll(Long currentUser) {
        List<SysNoticeUserEntity> list = sysNoticeReadRepository.findByUserId(currentUser);
        for (SysNoticeUserEntity sysNoticeUserEntity : list) {
            if (sysNoticeUserEntity.getUserId().longValue() == currentUser.longValue()) {
                sysNoticeUserEntity.setDeleteFlag(true);
            }
        }
        sysNoticeReadRepository.saveAll(list);
    }

}
